<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">管理员列表</div>
                <div class="layui-card-body">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-btn-group test-table-operate-btn">
                                <button class="layui-btn" data-type="addadmin">添加管理员</button>
                            </div>
                        </div>
                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate">

                        <script type="text/html" id="is_use_switch">
                            <input type="checkbox" name="is_use" lay-skin="switch" lay-text="启用|禁用" lay-filter="is_use_switch"
                                   value="{{ d.is_use }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_use == 1 ? 'checked' : '' }}>
                        </script>

                    </table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="editpass">修改密码</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<!--添加弹窗-->
<div id="addmodal" style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">登陆手机号</label>
            <div class="layui-input-block">
                <input type="text" name="h_admin_name" lay-verify="required|phone" placeholder="请输入登录手机号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">登陆密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="password" id="addpassword" placeholder="字母、数字、常用字符" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">重复密码</label>
            <div class="layui-input-block">
                <input type="password"  lay-verify="repassword"  placeholder="再次输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">管理员姓名</label>
            <div class="layui-input-block">
                <input type="text" name="admin_rename" lay-verify="required" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        {if !empty($role)}
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-inline">
                    <select name="role_id" lay-verify="required">
                        <option value="">选择角色</option>
                        {foreach $role as $k=>$v}
                        <option value="{$v['id']}">{$v['title']}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
        </div>
        {/if}
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addsubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!--修改弹窗-->
<div id="editmodal" style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">登陆手机号</label>
            <div class="layui-input-block">
                <input type="text" name="h_admin_name" lay-verify="required|phone" id="edit_h_admin_name" placeholder="请输入登录手机号" autocomplete="off" class="layui-input">
                <input type="hidden" name="h_admin_id"  id="edit_h_admin_id">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="admin_rename"  id="admin_rename" placeholder="请输入姓名" autocomplete="off" class="layui-input"> 
            </div>
        </div>
        {if !empty($role)}
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-inline">
                    <select name="role_id" lay-verify="required" id="role_id">
                        <option value="">选择角色</option>
                        {foreach $role as $k=>$v}
                        <option value="{$v['id']}">{$v['title']}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
        </div>
        {/if}

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editsubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!--修改密码-->
<div id="editpassmodal"  style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="editpassword" id="editpassword" placeholder="字母、数字、常用字符" autocomplete="off" class="layui-input">
                <input type="hidden" name="h_admin_id"  id="editPassdAminId">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" lay-verify="editrepassword"  placeholder="再次输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editpasssubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Admin/admin')}"
            ,height: 'full-155'
            ,cols: [[
                {field:'h_admin_name',align:'center',  title: '手机号码'}
                ,{field:'admin_rename',align:'center',  title: '姓名'}
                ,{field:'role_name', align:'center',  title: '角色',templet:function(d){
                        if(d.pid == 0){
                            return '超级管理员';
                        }else{
                            return d.role_name;
                        }
                    }}
                ,{field:'add_time',align:'center',   title: '添加时间'}
                ,{field:'last_login_time', align:'center',  title: '上次登陆时间',templet: function(d){
                        if (!d.last_login_time) {
                            return '未登录';
                        }else{
                            return d.last_login_time;
                        }
                    }}

                ,{field:'is_use',width:150, align:'center', title: '是否启用',templet: '#is_use_switch'}

                ,{align:'center',width:300, title: '操作',fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
        });

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });


        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'editpass'){

                if (data.pid == 0){
                    layer.msg('主账号密码不可修改',{icon:2,time:2000});
                    return false;
                }

                $('#editPassdAminId').val(data.h_admin_id);

                var editpassmodal = layer.open({
                    title:'修改密码'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['60%'] : ['500px']
                    ,content: $('#editpassmodal')
                });

            } else if(obj.event === 'del'){
                layer.confirm('确认删除管理员"' + data.h_admin_name + '"吗', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    if (data.pid == 0) {
                        layer.msg('主账号禁止删除',{icon:2,time:1000},function(){
                            layer.closeAll();
                        });

                    }else{
                        $.ajax({
                            url: "{:url('Admin/delAdmin')}",
                            type: 'post',
                            dataType: 'json',
                            data: {h_admin_id:data.h_admin_id},
                            success: function(msg){
                                layer.close(loading);
                                if (msg.code === 0) {
                                    obj.del();
                                    layer.close(index);
                                }else if (msg.code === 1){
                                    layer.msg('删除失败',{icon:2,time:1000});
                                }
                            }
                        })
                    }
                });
            } else if(obj.event === 'edit'){                           //修改操作
                $('#edit_h_admin_id').val(data.h_admin_id);
                $('#edit_h_admin_name').val(data.h_admin_name);
                $('#admin_rename').val(data.admin_rename); 
                form.render('select');
                layer.open({
                    title:'修改管理员信息'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,content: $('#editmodal')
                });
            }
        });

        //添加提交
        form.on('submit(addsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            $.ajax({
                url: "{:url('Admin/addAdmin')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){
                    if (data.code === 0) {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.msg,{icon:2,time:1000});
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //修改提交
        form.on('submit(editsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Admin/editAdmin')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(info){
                    if (info.code === 0) {
                        layer.msg('修改成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(info.code,{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //修改密码提交
        form.on('submit(editpasssubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Admin/editAdminPassword')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(info){
                    if (info.code === 0) {
                        layer.msg('修改成功',{icon:1,time:2000},function(){
                            layer.closeAll()
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('修改失败',{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //开关修改管理员状态
        form.on('switch(is_use_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            if (json.pid == 0){

                layer.msg('住账号不可禁用',{icon:2,time:1000},function(){
                    layer.close(loading);
                    window.location.reload();
                });

                return false;

            }

            var is_use = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Admin/editAdminIsUse')}",
                type: 'post',
                dataType: 'json',
                data: {h_admin_id:json.h_admin_id,is_use:is_use},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg('修改失败',{icon:2,time:1000},function(){
                            //layer.close(loading);
                            window.location.reload();
                        });
                    }
                }
            })
        });

        var $ = layui.$, active = {
            addadmin: function() {
                var addmodal = layer.open({
                    title: '添加员工'
                    , type: 1
                    , shadeClose: true
                    , area: admin.screen() < 2 ? ['80%'] : ['700px']
                    , content: $('#addmodal')
                });
            }

        };

        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //表单验证
        form.verify({
            admin_name:[
                /^[A-Za-z0-9]+$/
                ,'用户名只能包含字母和数字'
            ]
            ,password: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,editpassword:[
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]

            ,repassword:function(value){
                if(value == ''){
                    return '请再次输入密码';
                }
                var password = $('#addpassword').val();
                if (value != password) {
                    return '两次输入的密码不同';
                }
            }
            ,editrepassword:function(value){
                if (value == '') {
                    return '请再次输入密码';
                }
                var password = $('#editpassword').val();
                if (value != password) {
                    return '两次输入的密码不同';
                }
            }
        });

    });


    //格式化时间函数
    function Format(datetime,fmt) {
        if (parseInt(datetime)==datetime) {
            if (datetime.length==10) {
                datetime=parseInt(datetime)*1000;
            } else if(datetime.length==13) {
                datetime=parseInt(datetime);
            }
        }
        datetime=new Date(datetime);
        var o = {
            "M+" : datetime.getMonth()+1,                 //月份
            "d+" : datetime.getDate(),                    //日
            "h+" : datetime.getHours(),                   //小时
            "m+" : datetime.getMinutes(),                 //分
            "s+" : datetime.getSeconds(),                 //秒
            "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
            "S"  : datetime.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }

</script>


</body>
</html>